﻿//-----------------------------------------------------------------------
// <copyright file="Preference.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business.User
{
    using System;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Representation of a user preference
    /// </summary>
    [Serializable]
    public class Preference : IdentifierBase, IDataServicable
    {
        #region Members
        /// <summary>
        /// Preference name
        /// </summary>
        private string name;

        /// <summary>
        /// Preference value
        /// </summary>
        private string value;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Preference class.
        /// </summary>
        public Preference()
            : this(string.Empty, Guid.Empty, string.Empty)
        {       
        }

        /// <summary>
        /// Initializes a new instance of the Preference class.
        /// </summary>
        /// <param name="name">Preference name</param>
        /// <param name="userGuid">Preference owner</param>
        /// <param name="value">Preference value</param>
        public Preference(string name, Guid userGuid, string value)
            : base()
        {
            this.Name = name;
            this.UserGuid = userGuid;
            this.Value = value;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the unique identifier of the owning user.
        /// </summary>
        public virtual Guid UserGuid { get; set; }

        /// <summary>
        /// Gets or sets the name of the preference.
        /// </summary>
        public virtual string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Preference name must be a minimum of 1 characters.");
                }

                if (value.Length > 50)
                {
                    throw new InvalidLengthException("Preference name must be a maximum of 50 characters.");
                }

                this.name = value;
            }
        }

        /// <summary>
        /// Gets or sets the value of the preference.
        /// </summary>
        public virtual string Value
        {
            get
            {
                return this.value;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Preference value must be a minimum of 1 characters.");
                }

                if (value.Length > 255)
                {
                    throw new InvalidLengthException("Preference value must be a maximum of 255 characters.");
                }

                this.value = value;
            }
        }
        #endregion

        #region Static Load Methods
        /// <summary>
        /// Load preference by name
        /// </summary>
        /// <param name="name">Name of preference</param>
        /// <returns>User preference by name</returns>
        public static Preference Load(string name)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Name", name));
            return DataAccess.GetSingle<Preference>("LoadPreference", System.Data.CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Load preference by unique identifier
        /// </summary>
        /// <param name="guid">Unqiue identifier</param>
        /// <returns>User preference by unique identifier</returns>
        public static Preference Load(Guid guid)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", guid));
            return DataAccess.GetSingle<Preference>("LoadPreference", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Save Methods
        /// <summary>
        /// Save user to database
        /// </summary>
        public virtual void Save()
        {
            DataAccess.NonQuery("SavePreference", System.Data.CommandType.StoredProcedure, GetSaveParameters());
        }
        #endregion

        #region Delete Methods
        /// <summary>
        /// Delete preference from database
        /// </summary>
        public virtual void Delete()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            DataAccess.NonQuery("DeletePreference", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Fill Method
        /// <summary>
        /// Fill a preference object with data from a database
        /// </summary>
        /// <param name="reader">Database reader</param>
        public virtual void Fill(System.Data.IDataReader reader)
        {
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.Name = Helper.ToString(reader["Name"]);
            this.UserGuid = Helper.ToGuid(reader["UserGuid"]);
            this.Value = Helper.ToString(reader["Value"]);
        }
        #endregion

        #region Get Save Parameters
        public Parameters GetSaveParameters()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            parameters.Add(new Parameter("Name", this.Name));
            parameters.Add(new Parameter("UserGuid", this.UserGuid));
            parameters.Add(new Parameter("Value", this.Value));
            return parameters;
        }
        #endregion
    }
}
